﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Web_Pages_UserControls_EditInfoUserProfiles : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            xuatifo();
            Label1.Text = Application["count_visit"].ToString();
        }
    }

    void thuchiencapnhat()
    {

        UsersBLL userBll = new UsersBLL();
        Users user = userBll.GetIUsers.GetUserByUsername("User");
        UserProfiles userp = userBll.GetIUserProfiles.GetUserProfileOfUser(user);
        userp.Jobs = txtNghenghiep.Text;
        userp.Interests = txtSothich.Text;
        userp.User.Username = user.Username;
        txtTaikhoan.Text = user.Username;
        


        try
        {
            bool kq = userBll.GetIUserProfiles.UpdateUserProfile(userp);
            if (kq == true)
            {

            Response.Redirect(Request.UrlReferrer.ToString());
            Response.Write("<script>alert('Cap nhat thanh cong')</script>");
      
            }
            else
            {

            }
        }
        catch
        {
           Response.Write("<script>alert('Cap nhat khong thanh cong')</script>");
        }

    }
    protected void Lnknghenghiep_Click(object sender, EventArgs e)
    {
        txtTaikhoan.Text = "user";
        txtNghenghiep.Text = "";
        btkhong.Enabled = true;
        btOk.Enabled = true;
        txtNghenghiep.Focus();

        
       



    }

    protected void lnkSothich_Click(object sender, EventArgs e)
    {
        txtSothich.Text = "";
        txtTaikhoan.Text = "user";
        btkhong.Enabled = true;
        btOk.Enabled = true;
        txtSothich.Focus();

        
    }
    void xuatifo()
    {
        UsersBLL userBll = new UsersBLL();
        Users user = userBll.GetIUsers.GetUserByUsername("User");
        UserProfiles userp = userBll.GetIUserProfiles.GetUserProfileOfUser(user);


        try
        {
            this.txtTaikhoan.Text = userp.User.Username;
            txtEmail.Text = userp.User.Email;
            txtDantoc.Text = userp.People;
            txtSothich.Text = userp.Interests;
            txtNghenghiep.Text = userp.Jobs;
            txtThoigian.Text = userp.CreateTime.ToString();

            if (bool.Parse(userp.Gender.ToString()) == true)
            {
                checgtNa.Checked = true;
                checgtN.Visible = false;
                checgtNa.Enabled = false;
                checgtNa.Visible = true;
                checgtN.Enabled = false;
            }
            else
            {

                checgtN.Checked = true;
                checgtN.Visible = true;
                checgtNa.Enabled = false;
                checgtN.Enabled = false;
                checgtNa.Visible = false;
            }

        }
        catch
        {
            Response.Write("<script>alert('User khong hop le')</script>");
        }

    }
   
   
    protected void btOk_Click(object sender, EventArgs e)
    {
        thuchiencapnhat();
        btkhong.Enabled = false;
    }
    protected void btkhong_Click(object sender, EventArgs e)
    {
        UsersBLL userBll = new UsersBLL();
        Users user = userBll.GetIUsers.GetUserByUsername("User");
        UserProfiles userp = userBll.GetIUserProfiles.GetUserProfileOfUser(user);
        txtTaikhoan.Text = user.Username;
        txtSothich.Text = userp.Interests;
        txtNghenghiep.Text = userp.Jobs;
        btOk.Enabled = false;
    }
    protected void btnXemthongtin_Click(object sender, EventArgs e)
    {
        Response.Redirect("../../Web Pages/WebContents/Profiles.aspx");
    }
}